package com.qintian.rainstorm.modules.Controller;

import com.qintian.rainstorm.modules.entity.WaterRainBo;
import com.qintian.rainstorm.modules.sever.WaterRainServer;
import com.qintian.rainstorm.util.DateUtils;
import com.qintian.rainstorm.util.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * @description: 水利 首页控制层
 * @author: taoxi
 * @create: 2018-12-12 13:18
 **/
@RestController
@CrossOrigin
@RequestMapping(value = "/water")
public class WaterRainController {
    @Autowired
    WaterRainServer waterRainServer;


    /**
     * 水库/河道
     *
     * @param time
     * @return
     */
    @CrossOrigin
    @RequestMapping("/reservoirInfo")
    public R reservoirInfo(String time,String town) {

        Map<String, Object> objectMap = new HashMap<>();
        DecimalFormat df = new DecimalFormat("#########.##");
        //水库
        WaterRainBo waterRainBoSN = new WaterRainBo();
        WaterRainBo waterRainBoSD = new WaterRainBo();
        List<Map<String, Object>> listSn = new ArrayList<>();
        List<Map<String, Object>> listSo = new ArrayList<>();
        //河道
        WaterRainBo waterRainBoHN = new WaterRainBo();
        WaterRainBo waterRainBoHD = new WaterRainBo();
        if(town!=null && !town.equals("青田县")){
            waterRainBoSN.setTown(town);
            waterRainBoSD.setTown(town);
            waterRainBoHN.setTown(town);
            waterRainBoHD.setTown(town);
        }
        List<Map<String, Object>> listHn = new ArrayList<>();
        List<Map<String, Object>> listHo = new ArrayList<>();
        if (time != null && time != "") {
            Map<String, String> mapTime = GetTimeByFiveMinit(time);
            waterRainBoSN.setWaterType("Z");
            waterRainBoSN.setStartTime(mapTime.get("now"));
            waterRainBoHN.setWaterType("W");
            waterRainBoHN.setStartTime(mapTime.get("now"));

            waterRainBoSD.setWaterType("Z");
            waterRainBoSD.setStartTime(mapTime.get("old"));
            waterRainBoHD.setWaterType("W");
            waterRainBoHD.setStartTime(mapTime.get("old"));
            listSn = waterRainServer.selectWaterRainByTime(waterRainBoSN);
            listSo = waterRainServer.selectWaterRainByTime(waterRainBoSD);

            listHn = waterRainServer.selectWaterRainByTime(waterRainBoHN);
            listHo = waterRainServer.selectWaterRainByTime(waterRainBoHD);
        } else {
            waterRainBoSN.setWaterType("Z");
            waterRainBoHN.setWaterType("W");

            //获取数据库时间 判断是否为整五分钟时间
            String maxTime = waterRainServer.selectMaxTime();
            String minute = maxTime.substring(14, 16);
            int intMinute = Integer.parseInt(minute);
            int temp = intMinute % 5;
            if (temp != 0) {
                minute = intMinute - temp + "";
                minute = (minute.length() == 1 ? "0" + minute : minute);
            }
            maxTime = maxTime.substring(0, 14) + minute + maxTime.substring(16);

            waterRainBoSN.setStartTime(maxTime);
            waterRainBoHN.setStartTime(maxTime);
            listSn = waterRainServer.selectWaterRainByTime(waterRainBoSN);
            listHn = waterRainServer.selectWaterRainByTime(waterRainBoHN);
            if (listSn.size() > 0 && !listSn.isEmpty()) {
                String times = listSn.get(0).get("TM").toString();
                Map<String, String> mapTime = GetTimeByFiveMinit(GetParmtoString(times));
                waterRainBoSD.setWaterType("Z");
                waterRainBoSD.setStartTime(mapTime.get("old"));
                listSo = waterRainServer.selectWaterRainByTime(waterRainBoSD);

            }
            if (listHn.size() > 0 && !listHn.isEmpty()) {
                String times = listHn.get(0).get("TM").toString();
                Map<String, String> mapTime = GetTimeByFiveMinit(GetParmtoString(times));
                waterRainBoHD.setWaterType("W");
                waterRainBoHD.setStartTime(mapTime.get("old"));
                listHo = waterRainServer.selectWaterRainByTime(waterRainBoHD);

            }
        }
        for (int i = 0; i < listSn.size(); i++) {
            for (int m = 0; m < listSo.size(); m++) {
                Float now = Float.parseFloat(listSn.get(i).get("Z").toString());
                Float old = Float.parseFloat(listSo.get(i).get("Z").toString());
                if (now > old) {
                    listSn.get(i).put("sign", "+");
                } else if (now > old) {
                    listSn.get(i).put("sign", "-");
                } else {
                    listSn.get(i).put("sign", "=");
                }
            }
            Float max_z = Float.parseFloat(listSn.get(i).get("MAX_Z").toString());
            Float z = Float.parseFloat(listSn.get(i).get("Z").toString());
            Float res = z - max_z;
            if (res > 0) {
                listSn.get(i).put("err", "+" + df.format(res));
            } else {
                listSn.get(i).put("err", df.format(res));
            }
            Float ord = z / max_z;
            listSn.get(i).put("ord", df.format(ord));
            String type = listSn.get(i).get("TYPE").toString();
            String stcd = listSn.get(i).get("STCD").toString();
            String stnm = listSn.get(i).get("STNM").toString();
            listSn.get(i).put("STNM", stnm.replace(" ", ""));
            listSn.get(i).put("STCD", stcd.replace(" ", ""));
            listSn.get(i).put("TYPE", type.replace(" ", ""));
        }

        for (int i = 0; i < listHn.size(); i++) {
            for (int m = 0; m < listHo.size(); m++) {
                Float now = Float.parseFloat(listHn.get(i).get("Z").toString());
                Float old = Float.parseFloat(listHo.get(i).get("Z").toString());
                if (now > old) {
                    listHn.get(i).put("sign", "+");
                } else if (now < old) {
                    listHn.get(i).put("sign", "-");
                } else {
                    listHn.get(i).put("sign", "=");
                }
            }
            Float max_z = Float.parseFloat(listHn.get(i).get("MAX_Z").toString());
            Float z = Float.parseFloat(listHn.get(i).get("Z").toString());
            Float res = z - max_z;
            if (res > 0) {
                listHn.get(i).put("err", "+" + df.format(res));
            } else {
                listHn.get(i).put("err", df.format(res));
            }
            Float ord = z / max_z;
            listHn.get(i).put("ord", df.format(ord));
            String type = listHn.get(i).get("TYPE").toString();
            String stcd = listHn.get(i).get("STCD").toString();
            String stnm = listHn.get(i).get("STNM").toString();
            listHn.get(i).put("STNM", stnm.replace(" ", ""));
            listHn.get(i).put("STCD", stcd.replace(" ", ""));
            listHn.get(i).put("TYPE", type.replace(" ", ""));
        }

        objectMap.put("1", listSn);
        objectMap.put("2", listHn);
        return R.ok().put("DS", objectMap);
    }

    /**
     * 水库/河道 时间序列
     *
     * @param startime yymmddHHmm
     * @return
     */
    @CrossOrigin
    @RequestMapping("/reservoirByTime")
    public R reservoirByTime(String startime, String endtime) {
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Map<String, String> map = new HashMap<>();
        Date datestr = DateUtils.stringToDate(startime, DateUtils.DATE_TIME_PATTERN_2);
        Date datend = DateUtils.stringToDate(endtime, DateUtils.DATE_TIME_PATTERN_2);
        WaterRainBo waterRainBo = new WaterRainBo();
        waterRainBo.setStartTime(df.format(datestr));
        waterRainBo.setEndTime(df.format(datend));
        List<String> stringList = waterRainServer.selectWaterByTime(waterRainBo);
        return R.ok().put("DS", stringList);
    }


    /**
     * 水库/河道 站点 时间序列 水位图
     *
     * @param startime yymmddHHmm
     * @param num      时间间隔
     * @return
     */
    @CrossOrigin
    @RequestMapping("/reservoirByTimeAndStation")
    public R reservoirByTimeAndStation(String stationId, String startime, int num) {
        Map<String, String> map = new HashMap<>();
        if (num != 0) {
            map = GetTimeBy24Hour(startime, num);
        } else {
            map = GetTimeBy24Hour(startime, 24);
        }
        WaterRainBo waterRainBo = new WaterRainBo();
        waterRainBo.setStartTime(map.get("old"));
        waterRainBo.setEndTime(map.get("now"));
        waterRainBo.setStationId(stationId);
        List<Map<String, Object>> mapList = waterRainServer.selectWaterByTimeAndStation(waterRainBo);
        if (mapList.size() > 0) {
            for (int i = 0; i < mapList.size(); i++) {
                String type = mapList.get(i).get("TYPE").toString();
                String stcd = mapList.get(i).get("STCD").toString();
                String stnm = mapList.get(i).get("STNM").toString();
                mapList.get(i).put("STNM", stnm.replace(" ", ""));
                mapList.get(i).put("STCD", stcd.replace(" ", ""));
                mapList.get(i).put("TYPE", type.replace(" ", ""));
            }
        }

        return R.ok().put("DS", mapList);
    }

    /**
     * yymmddHHmm
     * 获取间隔5分钟的时间
     *
     * @param time
     * @return
     */
    private Map<String, String> GetTimeByFiveMinit(String time) {
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.000");
        Map<String, String> map = new HashMap<>();

        Date date = DateUtils.stringToDate(time, DateUtils.DATE_TIME_PATTERN_2);
        //现在/传入时间
        String nowTime = df.format(date);
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.add(Calendar.MINUTE, -5);
        String oldTime = df.format(calendar.getTime());
        map.put("now", nowTime);
        map.put("old", oldTime);

        return map;
    }

    /**
     * yymmddHHmm
     * 获取间隔 num 小时的时间
     *
     * @param time
     * @return
     */
    private Map<String, String> GetTimeBy24Hour(String time, int num) {
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.000");
        Map<String, String> map = new HashMap<>();

        Date date = DateUtils.stringToDate(time, DateUtils.DATE_TIME_PATTERN_2);
        //现在/传入时间
        String nowTime = df.format(date);
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.add(Calendar.HOUR, -num);
        String oldTime = df.format(calendar.getTime());
        map.put("now", nowTime);
        map.put("old", oldTime);

        return map;
    }

    /**
     * yymmddHHmm
     * 截取字符串
     *
     * @param time
     * @return
     */
    private String GetParmtoString(String time) {
        return time.replace(" ", "").replace("-", "").replace(":", "").substring(2, 12);

    }
}
